using UnityEngine;
using System.Collections;
using UnityEditor;

[AddComponentMenu ( "Chromatic2D/C2DAnimatedSprite" )]
public class C2DAnimatedSprite : C2DSprite 
{
	public C2DAnimation[] m_AnimationList;
	
	public bool m_bLoopAnimation 		= false;
	private bool m_bIsPlaying 			= false;
	
	private int m_uCurrentAnimation 	= 0;
	private int m_uCurrentFrame 		= 0;
	private float m_fCurrentFrameTime 	= 0f;
	private float m_fCurrentSpeed		= 1f;
	
	#if UNITY_EDITOR
	
	/// <summary>
	/// Creates an empty AnimatedSprite.
	/// </summary>
	[MenuItem("GameObject/Create Other/C2DAnimatedSprite")]
	public static void CreateC2DAnimatedSprite()
	{
		GameObject go = new GameObject("New Animated Sprite");
		go.AddComponent( typeof(C2DAnimatedSprite) );
	}
	
	#endif
	
	/// <summary>
	/// Update the animation.
	/// </summary>
	private void Update() 
	{
		if( m_bIsPlaying )
		{
			if( m_fCurrentFrameTime >= m_AnimationList[m_uCurrentAnimation].GetFrameDuration() )
			{
				m_fCurrentFrameTime = 0.0f;
				m_uCurrentFrame++;
				
				if( m_uCurrentFrame >= m_AnimationList[m_uCurrentAnimation].m_frameIndexList.Length )
				{
					m_uCurrentFrame = 0;
					
					if( m_bLoopAnimation )
					{
						Play( m_uCurrentAnimation );
					}
					else
					{
						m_bIsPlaying = false;
					}
				}
				else
				{
					SetSpriteIndex( m_AnimationList[m_uCurrentAnimation].m_frameIndexList[m_uCurrentFrame] );
				}
			}
			else
			{
				m_fCurrentFrameTime += Time.deltaTime * m_fCurrentSpeed;
			}	
		}
	}	
	
	/// <summary>
	/// Play the specified animation.
	/// </summary>
	public void Play( int AnimIndex )
	{
		Play( AnimIndex , 0 );
	}
	
	/// <summary>
	/// Play the specified animation from the specified frame
	/// </summary>
	public void Play( int AnimIndex, int startFrame )
	{
		m_bIsPlaying 			= true;
		m_uCurrentFrame 		= startFrame;
		m_uCurrentAnimation 	= AnimIndex;
		
		SetSpriteIndex( m_AnimationList[m_uCurrentAnimation].m_frameIndexList[m_uCurrentFrame] );
	}
	
	/// <summary>
	/// Stop the animation
	/// </summary>
	public void Stop()
	{
		m_bIsPlaying = false;
	}
	
	/// <summary>
	/// Determines whether an animation is playing.
	/// </summary>
	public bool IsPlaying()
	{
		return m_bIsPlaying;
	}
	
	/// <summary>
	/// Gets the current frame of the animation
	/// </summary>
	public int GetCurrentFrame()
	{
		return m_uCurrentFrame;
	}
	
	/// <summary>
	/// Get the current speed of the animation ( time multiplier )
	/// </summary>
	public float GetCurrentSpeed()
	{
		return m_fCurrentSpeed;
	}
	
	/// <summary>
	/// Sets the current speed of the animation ( time multiplier )
	/// </summary>
	/// </param>
	public void SetCurrentSpeed( float Speed )
	{
		m_fCurrentSpeed = Speed;
	}
	
	/// <summary>
	/// Warning calling this on a Animated sprite reset the animation list !
	/// </summary>
	public override void SetAtlas( UFTAtlasMetadata atlas )
	{
		Stop();
		m_AnimationList 		= null;	
		m_uCurrentAnimation 	= 0;
	 	m_uCurrentFrame 		= 0;
		m_fCurrentFrameTime 	= 0f;
		
		base.SetAtlas( atlas );
	}
}
